package com.binguo.service;

import com.binguo.common.exception.ServiceException;
import com.binguo.model.model.QucanguiUser;
import com.binguo.model.repository.QucanguiUserRepository;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yuritian
 * @Title: QucanguiUserService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020/4/2420:05
 */
@Service
public class QucanguiUserService {

    @Resource
    private QucanguiUserRepository qucanguiUserRepository;


    public void ModifyQucanguiUser(QucanguiUser qucanguiUser){

        Condition condition = new Condition(QucanguiUser.class);
        Example.Criteria criteria = condition.or();
        criteria.andEqualTo("branchId",qucanguiUser.getBranchId())
                .andEqualTo("phone",qucanguiUser.getPhone());
        if (qucanguiUser.getDeskId() != null){
            criteria.andEqualTo("deskId",qucanguiUser.getDeskId());
        }

        List<QucanguiUser> userList = qucanguiUserRepository.findByCondition(condition);

        if (null == userList || userList.size() == 0){
            qucanguiUser.setStatus(1);
            qucanguiUserRepository.save(qucanguiUser);
        }else {
            QucanguiUser oldUser = userList.get(0);
            oldUser.setStartTime(qucanguiUser.getStartTime());
            oldUser.setEndTime(qucanguiUser.getEndTime());
            oldUser.setName(qucanguiUser.getName());
            oldUser.setStatus(1);
            qucanguiUserRepository.update(oldUser);
        }

    }

    public void deleteQucanguiUser(Integer branchId,
                                   Integer deskId,
                                   String phone,
                                   Integer status){
        Condition condition = new Condition(QucanguiUser.class);
        Example.Criteria criteria = condition.or();
        criteria.andEqualTo("branchId",branchId)
                .andEqualTo("phone",phone);
        if (deskId != null){
            criteria.andEqualTo("deskId",deskId);
        }

        List<QucanguiUser> userList = qucanguiUserRepository.findByCondition(condition);

        if (null == userList || userList.size() == 0) {
            throw new ServiceException("不存在该存餐柜管理员");
        }else {
            QucanguiUser user = userList.get(0);
            user.setStatus(status);
            qucanguiUserRepository.update(user);
        }
    }

    public List<QucanguiUser> getUserList(Integer branchId,
                                          Integer deskId){
        List<QucanguiUser> result;

        Condition condition = new Condition(QucanguiUser.class);
        Example.Criteria criteria = condition.or();
        criteria.andEqualTo("branchId",branchId);
        if (deskId != null){
            criteria.andEqualTo("deskId",deskId);
        }
        result = qucanguiUserRepository.findByCondition(condition);
        return result;
    }

    public void queryCustomerStatus(Integer branchId,
                                    Integer deskId,
                                    String phone){


        Condition condition = new Condition(QucanguiUser.class);
        Example.Criteria criteria = condition.or();
        criteria.andEqualTo("branchId",branchId)
                .andEqualTo("phone",phone)
                .andEqualTo("status",1);
        if (deskId != null){
            criteria.andEqualTo("deskId",deskId);
        }
        List<QucanguiUser> result = qucanguiUserRepository.findByCondition(condition);
        if (null == result || result.size() == 0){
            throw new ServiceException("该用户未拥有权限");
        }
        QucanguiUser user = result.get(0);
        if (user.getEndTime().before(new Date())){
            user.setStatus(0);
            qucanguiUserRepository.update(user);
            throw new ServiceException("该用户未拥有权限");
        }
    }
}
